﻿package com.shuqi.activity;

import java.net.InetAddress;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.activity.viewport.CommonTitle;
import com.shuqi.common.Config;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.ScanLocalFolderTools;
import com.shuqi.common.utils.Util;
import com.shuqi.controller.R;
import com.shuqi.swiftp.Defaults;
import com.shuqi.swiftp.FTPServerService;
import com.shuqi.swiftp.Globals;

/**
 * wifi传输功能
 * 
 * @author Administrator
 * 
 */
public class FTPTransferActivity extends ActivityBase implements OnClickListener {

    private TextView ipTipsTextView;
    private CommonTitle mTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_ftp_transfer);
        mTitle = (CommonTitle) findViewById(R.id.title);
        mTitle.setLeftClickListener(this);
        ipTipsTextView = (TextView) findViewById(R.id.ftp_input_ip_tv);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Globals.setContext(this);

        // 判定SD卡是否可用
        if (!ScanLocalFolderTools.isCanUseSdCard()) {
            findViewById(R.id.hint_nosdcard).setVisibility(View.VISIBLE);
            findViewById(R.id.ftp_content_scroll).setVisibility(View.GONE);
            return;
        } else {
            findViewById(R.id.hint_nosdcard).setVisibility(View.GONE);
            findViewById(R.id.ftp_content_scroll).setVisibility(View.VISIBLE);
        }
        if (!Util.isNetworkConnected(FTPTransferActivity.this) ||
                !"wifi".equals(Config.getNetType(FTPTransferActivity.this))) {
            findViewById(R.id.ftp_toast_lin).setVisibility(View.GONE);
            findViewById(R.id.ftp_nonet_toast_tv).setVisibility(View.VISIBLE);
            ((TextView) findViewById(R.id.ftp_toast_tv1)).setText("WIFI服务未启用");
            ((TextView) findViewById(R.id.ftp_toast_tv2)).setText("请检查您的WIFI状态");
            ((ImageView) findViewById(R.id.ftp_signal_img))
                    .setBackgroundResource(R.drawable.icon_wifi_no_net);
            return;
        }else {// 开启服务
            findViewById(R.id.ftp_toast_lin).setVisibility(View.VISIBLE);
            findViewById(R.id.ftp_nonet_toast_tv).setVisibility(View.GONE);
            ((TextView) findViewById(R.id.ftp_toast_tv1)).setText("WIFI服务已开启");
            ((TextView) findViewById(R.id.ftp_toast_tv2)).setText("不用数据线也能将文件传到手机上");
            ((ImageView) findViewById(R.id.ftp_signal_img))
                    .setBackgroundResource(R.drawable.icon_wifi_has_net);
            InetAddress address = FTPServerService.getWifiIp();
            ipTipsTextView.setText("ftp://" + address.getHostAddress() + ":" + Defaults.portNumber + "/");
            Intent intent = new Intent(this, FTPServerService.class);
            startService(intent);
        }
    }

    @Override
    protected void onStop() {
        Intent intent = new Intent(this, FTPServerService.class);
        stopService(intent);
        Globals.setContext(null);
        super.onStop();
    }

    @Override
    public void activityLogicForward() {}

    @Override
    public void activityInitData() {}

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left:
                ActivityManager.getInstance().finishActivity(this);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            ActivityManager.getInstance().finishActivity(this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
